package com.qinggeng.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.GoodsKeywordsDto;
import com.qinggeng.project.system.domain.Goods;
import com.qinggeng.project.system.domain.GoodsKeywords;
import com.qinggeng.project.system.mapper.GoodsKeywordsMapper;
import com.qinggeng.project.system.service.IGoodsKeywordsService;
import com.qinggeng.project.system.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@Service
public class GoodsKeywordsServiceImpl implements IGoodsKeywordsService {

    @Resource
    private GoodsKeywordsMapper keywordsMapper;

    @Autowired
    private IGoodsService goodsService;

    @Override
    public int insertGoodsKeywords(GoodsKeywords keywords) {
        return keywordsMapper.insertGoodsKeywords(keywords);
    }

    @Override
    public AjaxResult insertGoodsKeywords(String inputWord, String goodsName) {
        if(StringUtils.isEmpty(inputWord)){
            return AjaxResult.error("至少需要填写一个关键词");
        }
        int rows=0;//数据库受影响行数
        List<String> notAddWordList=new ArrayList<>();//商品名称不包含的关键词集合
        String[] wordArray=inputWord.split(",");
        for (String word:wordArray//根据“逗号”拆分关键词
             ) {
            if(goodsName.contains(word)){//商品名称包含此关键词
                if(selectGoodsKeywordsByKeyword(word)==null){//数据库中没有此关键词
                    rows=insertGoodsKeywords(new GoodsKeywords(word))>0?++rows:rows;
                }
            }else{//商品名称不包含关键词
                notAddWordList.add(word);//添加到集合中
            }
        }
        if(notAddWordList.size()==wordArray.length){
            return AjaxResult.error("没有匹配的关键词");
        }
        if(notAddWordList.size()>0){//如果集合中有不包含的关键词
            return AjaxResult.success("商品名称不包含关键词："+notAddWordList.toString(),rows);
        }
        return AjaxResult.success(rows);
    }

    @Override
    public int deleteGoodsKeywordsById(Long id) {
        return keywordsMapper.deleteGoodsKeywordsById(id);
    }

    /**
     * 自动删除关键词
     * @param goodsKeywords
     */
    @Override
    public int deleteGoodsKeywordsAuto(GoodsKeywords goodsKeywords) {
        int rows=0;
        /**
         * 根据联想的关键词查询商品
         */
        Goods where=new Goods();
        where.setDelFalg("10");//删除标记：10=未标记
        where.setStatus("20");//商品状态：20=已上架
        where.setGoodsName(goodsKeywords.getKeyword());
        PageHelper.startPage(0,1);
        List<Goods> goodsList=goodsService.selectGoodsList(where);
        /**
         * //如果该关键词的搜索商品为空，删除该关键词
         */
        if(goodsList==null||goodsList.size()==0){
            rows=deleteGoodsKeywordsById(goodsKeywords.getId());
        }
        return rows;
    }

    @Override
    public int updateGoodsKeywords(GoodsKeywords keywords) {
        return keywordsMapper.updateGoodsKeywords(keywords);
    }


    @Override
    public GoodsKeywords selectGoodsKeywordsByKeyword(String keyword) {
        return keywordsMapper.selectGoodsKeywordsByKeyword(keyword);
    }

    @Override
    public List<GoodsKeywords> selectGoodsKeywordsList(GoodsKeywords goodsKeywords) {
        return keywordsMapper.selectGoodsKeywordsList(goodsKeywords);
    }

    @Override
    public List<GoodsKeywordsDto> selectGoodsKeywordsListDtoByKeyword(String keyword) {

        return keywordsMapper.selectGoodsKeywordsListDtoByKeyword(keyword);
    }

    @Override
    public int insertGoodsKeywordsList(List<String> keywordsList) {
        return keywordsMapper.insertGoodsKeywordsList(keywordsList);
    }
}
